package kyFtp
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	
	import kyFtp.events.FtpEvent;
	import kyFtp.events.RequestEvent;
	import kyFtp.process.LoginProcess;
	import kyFtp.process.ProcessBase;
	import kyFtp.process.UploadProcess;
	import kyFtp.response.Response;
	import kyFtp.utils.ExtendSocket;
	
	public class KyFtpClient extends EventDispatcher
	{
		private var controlSocket:ExtendSocket;
		private var currentProcess:ProcessBase;
		private var pausedProcess:ProcessBase;
		private var _loginProcess:LoginProcess;
		public function KyFtpClient()
		{
			controlSocket = new ExtendSocket();
			controlSocket.addEventListener(Event.CLOSE, closeSock);
			controlSocket.addEventListener(IOErrorEvent.IO_ERROR, closeSock);
			controlSocket.addEventListener(Event.CONNECT, onConnectSock);
			controlSocket.addEventListener(ProgressEvent.SOCKET_DATA, onGetSockData);
			
			_loginProcess = new LoginProcess();
			_loginProcess.addEventListener(FtpEvent.LOGIN_SUCCESS,loginSuccess);
		}
		public function get host():String{return controlSocket.host; }
		public function get port():int{return controlSocket.port; }
		public function get loginProcess():LoginProcess{ return _loginProcess; }
		public function login(user:String,password:String,host:String,port:int=21):void
		{
			trace("开始登陆");
			removeListener();
			_loginProcess.user = user;
			_loginProcess.pwd = password;
			currentProcess = _loginProcess;
			addListeners();
			controlSocket.host = host;
			controlSocket.port = port;
			controlSocket.connect(host,port);
		}
		public function loginAndUpload(user:String,password:String,host:String,port:int,uploadProcess:UploadProcess):void
		{
			pausedProcess = uploadProcess;
			login(user,password,host,port);
		}
		public function closeSock(e:Event=null):void
		{
			trace("连接断开");
			_loginProcess.isLogin = false;
			if(e is IOErrorEvent) 
			{
				dispatchEvent(new FtpEvent(FtpEvent.LOGIN_FAIL));
			}
			else
			{
				controlSocket.close();
			}
		}
		private function onErrorSock(e:IOErrorEvent):void
		{
			trace("错误:"+e.errorID);
		}
		private function onConnectSock(e:Event):void
		{
			trace("连接成功");
		}
		private function onGetSockData(e:ProgressEvent):void
		{
			if(currentProcess)
			{
				var response:String = controlSocket.readUTFBytes(controlSocket.bytesAvailable);
				trace("返回:"+response+"------------------------------------------------------");
				var ar:Array = response.match(/\d{3}[\s-]?.*/g);
				for(var i:int=0 ; i<ar.length ; i++)
				{
					currentProcess.responseHandler(Response.parse(ar[i]));
				}
			}
		}
		private function loginSuccess(e:FtpEvent):void
		{
			if(pausedProcess)
			{
				executeProcess(pausedProcess);
				pausedProcess = null;
			}
		}
		public function executeProcess(process:ProcessBase):void
		{
			cleanProcess();
			currentProcess = process;
			addListeners();
			currentProcess.sendRequest();
		}
		private function cleanProcess():void
		{
			if(currentProcess)
			{
				removeListener();
			}
			currentProcess = null;
		}
		private function addListeners():void
		{
				currentProcess.addEventListener(RequestEvent.SEND_CMD, SendCMDHandler);
		}
		private function removeListener():void
		{
			if(currentProcess && currentProcess.hasEventListener(RequestEvent.SEND_CMD))
				currentProcess.removeEventListener(RequestEvent.SEND_CMD, SendCMDHandler);
		}
		private function SendCMDHandler(e:RequestEvent):void
		{
			if(controlSocket.connected)
			{
				trace("发送:"+e.request.toCmdStr());
				controlSocket.writeUTFBytes(e.request.toCmdStr()+"\r\n");
				controlSocket.flush();
			}
			else
			{
				pausedProcess = currentProcess;
				login(_loginProcess.user,_loginProcess.pwd,controlSocket.host,controlSocket.port);
			}
		}
	}
}